#include "pagewheelset.h"
#include "ui_pagewheelset.h"

PageWheelSet::PageWheelSet(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageWheelSet)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    m_pNumKeyBoard = new NumberKeyBoard(this);
    m_pNumKeyBoard->move(850,230);
    connect(m_pNumKeyBoard,SIGNAL(setNumber(int)),this,SLOT(input_slot(int)));
    connect(m_pNumKeyBoard,SIGNAL(backspace()),this,SLOT(backspace_slot()));
    connect(m_pNumKeyBoard,SIGNAL(cancel()),this,SLOT(cancel_slot()));
    m_pNumKeyBoard->hide();

    inputlist.append(ui->lineEdit_Pro2);
    inputlist.append(ui->lineEdit_Pro3);
    inputlist.append(ui->lineEdit_Pro4);
    inputlist.append(ui->lineEdit_Pro5);
    inputlist.append(ui->lineEdit_Brk_Uinit_1);
    inputlist.append(ui->lineEdit_Brk_Uinit_2);
    for(int i=0; i<inputlist.size(); ++i)
    {
        inputlist.at(i)->installEventFilter(this);
        QRegExp rx = QRegExp("^(7[7-9][0-9]|8[0-3][0-9]|840)$");
        QRegExpValidator *validator = new QRegExpValidator(rx,this);
        inputlist[i]->setValidator(validator);
    }

    setPageActive(false);

    m_lProWheelList.append(ui->ProWheel_1);
    m_lProWheelList.append(ui->ProWheel_2);
    m_lProWheelList.append(ui->ProWheel_3);
    m_lProWheelList.append(ui->ProWheel_4);

    m_lBrkWheelList.append(ui->BrkWheel_Uinit_1);
    m_lBrkWheelList.append(ui->BrkWheel_Uinit_2);
}

PageWheelSet::~PageWheelSet()
{
    delete ui;
}

void PageWheelSet::setPageActive(bool xActive)
{
    ui->Btn_Confirm_BRK->setEnabled(xActive);
    ui->Btn_Confirm_TRAC->setEnabled(xActive);
}

bool PageWheelSet::eventFilter(QObject *obj, QEvent *event)
{
    for(int i=0; i<inputlist.size(); ++i)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            if(obj == inputlist.at(i))
            {
                inputlist.at(i)->setStyleSheet("background-color: rgb(100,255,255); border: 1px solid white;font:20px 'Arial';");
                m_pNumKeyBoard->show();
                m_pFocusLinedit = inputlist.at(i);
            }
            else
                inputlist.at(i)->setStyleSheet("background-color: white; border: 1px solid white;font:20px 'Arial';");
        }
    }
    return QWidget::eventFilter(obj, event);
}

void PageWheelSet::input_slot(int number)
{
    if(m_pFocusLinedit == NULL)
        return;
    QKeyEvent numKey(QEvent::KeyPress, (Qt::Key)(Qt::Key_0+number), Qt::NoModifier, QString::number(number));
    QApplication::sendEvent(m_pFocusLinedit, &numKey);
}

void PageWheelSet::backspace_slot()
{
    if(m_pFocusLinedit == NULL)
        return;
    QKeyEvent backspaceKey(QEvent::KeyPress,Qt::Key_Backspace,Qt::NoModifier);
    QApplication::sendEvent(m_pFocusLinedit,&backspaceKey);
}

void PageWheelSet::cancel_slot()
{
    m_pNumKeyBoard->hide();
}

void PageWheelSet::on_Btn_Return_clicked()
{
    emit returnToMaintMenu();
}

void PageWheelSet::setProWheelValue(uint8_t index,uint8_t value)
{
    if(index >= m_lProWheelList.length())
        return;
    m_lProWheelList[index]->setText(QString("%1").arg(value+770));
}

void PageWheelSet::setBrkWheelValue(uint8_t index,uint8_t value)
{
    if(index >= m_lBrkWheelList.length())
        return;
    m_lBrkWheelList[index]->setText(QString("%1").arg(value+770));
}

uint8_t PageWheelSet::getProWheelInputValue(uint8_t index)
{
    return inputlist[index]->text().toUShort()-770;
}

uint8_t PageWheelSet::getBrkWheelInputValue(uint8_t index)
{
    return inputlist[4+index]->text().toUShort()-770;
}

void PageWheelSet::on_Btn_Confirm_TRAC_clicked()
{
    emit signal_ProWheelSetValid(true);
    ui->Btn_Confirm_TRAC->setEnabled(false);
    QTimer::singleShot(3000,this,[=](){
        emit signal_ProWheelSetValid(false);
        ui->Btn_Confirm_TRAC->setEnabled(true);
    });
}

void PageWheelSet::on_Btn_Confirm_BRK_clicked()
{
    emit signal_BrkWheelSetValid(true);
    ui->Btn_Confirm_BRK->setEnabled(false);
    QTimer::singleShot(3000,this,[=](){
        emit signal_BrkWheelSetValid(false);
        ui->Btn_Confirm_BRK->setEnabled(true);
    });
}
